<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
      
    </BasicTable>
    <!-- 表单区域 -->
    <CsCourseClazzModal @register="registerModal" @success="handleSuccess"></CsCourseClazzModal>
  </div>
</template>

<script lang="ts" name="course-csCourseClazz" setup>
  import {reactive} from 'vue';
  import {BasicTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import CsCourseClazzModal from './components/CsCourseClazzModal.vue'
  import {columns, searchFormSchema} from './CsCourseClazz.data';
  import {list, deleteOne,switchStatus} from './CsCourseClazz.api';
  const queryParam = reactive<any>({});
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { tableContext } = useListPage({
      tableProps:{
           title: '课程分类',
           api: list,
           columns,
           canResize:false,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 160,
               fixed:'right'
            },
            beforeFetch: (params) => {
              return Object.assign(params, queryParam);
            },
      }
  })

  const [registerTable, {reload},{ selectedRowKeys }] = tableContext


   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
  
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({id: record.id}, handleSuccess);
   }

   /**
    * 启用/停用
    */
  async function changeStatus(record,status) {
     await switchStatus({id: record.id,status:status}, handleSuccess);
   }
 
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
    let menu = new Array();
		if(record.status==0) {
		   menu.push({label: '启用',onClick: changeStatus.bind(null, record,1)});
		}else {
		   menu.push({
			  label: '停用',
			  popConfirm: {
				title: '是否确认停用',
				confirm: changeStatus.bind(null, record,0),
				placement: 'topLeft',
			  }
		   });
		}
		menu.push({label: '编辑',onClick: handleEdit.bind(null, record)});
		menu.push({label: '删除',onClick: handleDelete.bind(null,record)});
		return menu; 
   }

</script>

<style scoped>

</style>